package com.wp.file.utils;

import com.wp.file.myoffice.bean.Document;
import org.apache.commons.lang.ArrayUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileSort {
    public static final String[] DOC = {"doc", "docx", "ppt", "pptx", "xls", "xlsx", "pdf"};
    public static final String[] IMG = {"jpg", "png", "gif", "jpeg"};
    public static final String[] AUDIO = {"wav", "mp3", "wma", "aac", "flac"};
    public static final String[] VIDEO = {"avi", "mp4","mov"};
    public static final String[] ZIP = {"zip" , "rar" ,"7z"};

    /**
     * 判断文件类型
     */
    public static Integer getFileType(String filename){
        String extendName = FileSort.getExtendName(filename);
        List<String[]> typelist = Arrays.asList(DOC, IMG, AUDIO, VIDEO, ZIP);
        for (int i = 0; i < typelist.size(); i++) {
            for (String type : typelist.get(i)) {
                if(extendName.equalsIgnoreCase(type)){
                    return i+1;
                }
            }
        }
        return 10;//10就是其他
    }

    /**
     * 获取文件扩展名
     * @param filename
     * @return
     */
    public static String getExtendName(String filename){
        if (filename.lastIndexOf(".")==-1){
            return "";
        }

        return filename.substring(filename.lastIndexOf(".")+1);
    }

    /**
     * office文档能否编辑
     * @param document
     * @return
     */
    public static boolean canEdit(Document document) {
        String[] noCanEdit = {"pdf"};
        if (ArrayUtils.contains(noCanEdit, document.getFileType())) {
            return false;
        }
        return true;
    }

}
